package com.brkj.test;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import com.brkj.httpInterface.BaseAjaxParams;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.brkj.model.DS_Vote;
import com.brkj.model.DS_Vote_answer;
import com.brkj.model.DS_Vote_detail_ques;
import com.brkj.main.R;
import com.brkj.util.ConstAnts;
import com.brkj.util.JsonUtil;
import com.brkj.httpInterface.HttpInterface.HIF_SubmitVote;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.httpInterface.HttpInterface.HIF_GetVoteDetail;
import com.brkj.util.view.BaseActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class VoteDetailActivity extends BaseActivity{

	@ViewInject(id=R.id.question) TextView questionText;
	@ViewInject(id=R.id.selectView) View selectView;
	@ViewInject(id=R.id.selectA) RadioButton selectA;
	@ViewInject(id=R.id.selectB) RadioButton selectB;
	@ViewInject(id=R.id.selectC) RadioButton selectC;
	@ViewInject(id=R.id.selectD) RadioButton selectD;
	@ViewInject(id=R.id.selectE) RadioButton selectE;
	@ViewInject(id=R.id.multiSelectView) View multiSelectView;
	@ViewInject(id=R.id.multiSelectA) CheckBox multiSelectA;
	@ViewInject(id=R.id.multiSelectB) CheckBox multiSelectB;
	@ViewInject(id=R.id.multiSelectC) CheckBox multiSelectC;
	@ViewInject(id=R.id.multiSelectD) CheckBox multiSelectD;
	@ViewInject(id=R.id.multiSelectE) CheckBox multiSelectE;
	@ViewInject(id=R.id.asnEditText) EditText asnEditText;
	@ViewInject(id=R.id.preBtn) Button preBtn;
	@ViewInject(id=R.id.nextBtn) Button nextBtn;
	
	private String voteID;
	 
	private List<DS_Vote_detail_ques> quesList;
	private List<DS_Vote_answer> answerList =  new ArrayList<DS_Vote_answer>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setAnimType(BaseActivity.RIGHT_ENTER_LEFT_EXT);
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.vote_detail);
		setActivityTitle("问卷调查");
		setReturnBtn();
		
		voteID = Integer.toString(((DS_Vote)getIntent().getSerializableExtra("vote")).getVotePaperID());
		
		getVoteDetail();
		
//		onListen();
	}


	private void getVoteDetail() {
		BaseAjaxParams params = new BaseAjaxParams();
		params.put(HIF_GetVoteDetail.params.VotePaperID, voteID);
		
		FinalHttp fh = new FinalHttp();
		fh.post(HIF_GetVoteDetail.address, params, new MyAjaxCallBack<Object>(this) {

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				
				Gson gson = new Gson();
				Type listType = new TypeToken<List<DS_Vote_detail_ques>>(){}.getType();
				quesList = gson.fromJson((String)t, listType);
				
				if(quesList != null){
					showQuestion(quesList.get(0), 0);
				}else{
					showToast("获取问卷失败！");
				}
			}
			
		});
	}

	protected void showQuestion(final DS_Vote_detail_ques question, final int pos) {
		
		questionText.setText(question.getNum() + "." + question.getQuestion());
		
		if(question.getType() == ConstAnts.VOTE_SINGLE_SELECT){
			
			asnEditText.setVisibility(View.GONE);
			selectView.setVisibility(View.VISIBLE);
			multiSelectView.setVisibility(View.GONE);
			
			selectA.setText(question.getSelectA());
			selectB.setText(question.getSelectB());
			selectC.setText(question.getSelectC());
			selectD.setText(question.getSelectD());
			if(question.getSelectE() == ""){
				selectE.setVisibility(View.GONE);
			}else{
				selectE.setVisibility(View.VISIBLE);
				selectE.setText(question.getSelectE());
			}
		}else if(question.getType() == ConstAnts.VOTE_MULTI_SELECT){
			asnEditText.setVisibility(View.GONE);
			selectView.setVisibility(View.GONE);
			multiSelectView.setVisibility(View.VISIBLE);
			
			multiSelectA.setText(question.getSelectA());
			multiSelectB.setText(question.getSelectB());
			multiSelectC.setText(question.getSelectC());
			multiSelectD.setText(question.getSelectD());
			if(question.getSelectE() != null){
				if(question.getSelectE().equals("")){
					multiSelectE.setVisibility(View.GONE);
				}else{
					multiSelectE.setVisibility(View.VISIBLE);
					multiSelectE.setText(question.getSelectE());
				}
			}else{
				multiSelectE.setVisibility(View.GONE);
			}

			
		}else{
			asnEditText.setVisibility(View.VISIBLE);
			selectView.setVisibility(View.GONE);
			multiSelectView.setVisibility(View.GONE);
		}
		
		if(pos == 0){
			preBtn.setVisibility(View.GONE);
		}else{
			preBtn.setVisibility(View.VISIBLE);
			preBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					saveAnswer(question);
					showQuestion(quesList.get(pos-1), pos-1);
				}
			});
		}
		
		
		if(pos == (quesList.size() -1)){
			nextBtn.setText("提交");
			nextBtn.setBackgroundResource(R.drawable.btn_2_selector);
			nextBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					submitAnswer();
					
				}
			});
		}else{
			nextBtn.setText("下一题");
			nextBtn.setBackgroundResource(R.drawable.btn_4_selector);
			nextBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					saveAnswer(question);
					showQuestion(quesList.get(pos+1), pos+1);
					
				}
			});
		}
		
		checkAnswer(question);
	}

	private void checkAnswer(DS_Vote_detail_ques question) {
//		cleaSelect();
		
		for(DS_Vote_answer answer : answerList){
			if(answer.getUTID() == question.getUTID()){
				if(question.getType() == ConstAnts.VOTE_SINGLE_SELECT){
					if(answer.getAnswer().equals("A")){
						selectA.setChecked(true);
						selectB.setChecked(false);
						selectC.setChecked(false);
						selectD.setChecked(false);
						selectE.setChecked(false);
						
						multiSelectA.setChecked(false);
						multiSelectB.setChecked(false);
						multiSelectC.setChecked(false);
						multiSelectD.setChecked(false);
						multiSelectE.setChecked(false);
						
						asnEditText.setText("");
						return;
					}else if(answer.getAnswer().equals("B")){
						selectB.setChecked(true);
						selectA.setChecked(false);
						selectC.setChecked(false);
						selectD.setChecked(false);
						selectE.setChecked(false);
						
						multiSelectA.setChecked(false);
						multiSelectB.setChecked(false);
						multiSelectC.setChecked(false);
						multiSelectD.setChecked(false);
						multiSelectE.setChecked(false);
						
						asnEditText.setText("");
						return;
					}else if(answer.getAnswer().equals("C")){
						selectC.setChecked(true);
						selectA.setChecked(false);
						selectB.setChecked(false);
						selectD.setChecked(false);
						selectE.setChecked(false);
						
						multiSelectA.setChecked(false);
						multiSelectB.setChecked(false);
						multiSelectC.setChecked(false);
						multiSelectD.setChecked(false);
						multiSelectE.setChecked(false);
						
						asnEditText.setText("");
						return;
					}else if(answer.getAnswer().equals("D")){
						selectD.setChecked(true);
						selectA.setChecked(false);
						selectB.setChecked(false);
						selectC.setChecked(false);
						selectE.setChecked(false);
						
						multiSelectA.setChecked(false);
						multiSelectB.setChecked(false);
						multiSelectC.setChecked(false);
						multiSelectD.setChecked(false);
						multiSelectE.setChecked(false);
						
						asnEditText.setText("");
						return;
					}else if(answer.getAnswer().equals("E")){
						selectE.setChecked(true);
						selectA.setChecked(false);
						selectB.setChecked(false);
						selectC.setChecked(false);
						selectD.setChecked(false);
						
						multiSelectA.setChecked(false);
						multiSelectB.setChecked(false);
						multiSelectC.setChecked(false);
						multiSelectD.setChecked(false);
						multiSelectE.setChecked(false);
						
						asnEditText.setText("");
						return;
					}
				}else if(question.getType() == ConstAnts.VOTE_MULTI_SELECT){
					String answerStr = answer.getAnswer();
					
					if(answerStr.contains("A")){
						multiSelectA.setChecked(true);
						selectA.setChecked(false);
						selectB.setChecked(false);
						selectC.setChecked(false);
						selectD.setChecked(false);
						selectE.setChecked(false);
						
						multiSelectB.setChecked(false);
						multiSelectC.setChecked(false);
						multiSelectD.setChecked(false);
						multiSelectE.setChecked(false);
						
						asnEditText.setText("");
					}
					
					if(answerStr.contains("B")){
						multiSelectB.setChecked(true);
						selectA.setChecked(false);
						selectB.setChecked(false);
						selectC.setChecked(false);
						selectD.setChecked(false);
						selectE.setChecked(false);
						
						multiSelectA.setChecked(false);
						multiSelectC.setChecked(false);
						multiSelectD.setChecked(false);
						multiSelectE.setChecked(false);
						
						asnEditText.setText("");
					}
					
					if(answerStr.contains("C")){
						multiSelectC.setChecked(true);
						selectA.setChecked(false);
						selectB.setChecked(false);
						selectC.setChecked(false);
						selectD.setChecked(false);
						selectE.setChecked(false);
						
						multiSelectA.setChecked(false);
						multiSelectB.setChecked(false);
						multiSelectD.setChecked(false);
						multiSelectE.setChecked(false);
						
						asnEditText.setText("");
					}
					
					if(answerStr.contains("D")){
						multiSelectD.setChecked(true);
						selectA.setChecked(false);
						selectB.setChecked(false);
						selectC.setChecked(false);
						selectD.setChecked(false);
						selectE.setChecked(false);
						
						multiSelectA.setChecked(false);
						multiSelectB.setChecked(false);
						multiSelectC.setChecked(false);
						multiSelectE.setChecked(false);
						
						asnEditText.setText("");
					}
					
					if(answerStr.contains("E")){
						multiSelectE.setChecked(true);
						selectA.setChecked(false);
						selectB.setChecked(false);
						selectC.setChecked(false);
						selectD.setChecked(false);
						selectE.setChecked(false);
						
						multiSelectA.setChecked(false);
						multiSelectB.setChecked(false);
						multiSelectC.setChecked(false);
						multiSelectD.setChecked(false);
						
						asnEditText.setText("");
					}
					
					return;
				}else if (question.getType() == ConstAnts.VOTE_FILL_BLANK){
					asnEditText.setText(answer.getAnswer());
					selectA.setChecked(false);
					selectB.setChecked(false);
					selectC.setChecked(false);
					selectD.setChecked(false);
					selectE.setChecked(false);
					
					multiSelectA.setChecked(false);
					multiSelectB.setChecked(false);
					multiSelectC.setChecked(false);
					multiSelectD.setChecked(false);
					multiSelectE.setChecked(false);
					
					return;
				}
				
			}
		}
		
	}

	private void cleaSelect() {
		
		selectA.setChecked(false);
		selectB.setChecked(false);
		selectC.setChecked(false);
		selectD.setChecked(false);
		selectE.setChecked(false);
		
		multiSelectA.setChecked(false);
		multiSelectB.setChecked(false);
		multiSelectC.setChecked(false);
		multiSelectD.setChecked(false);
		multiSelectE.setChecked(false);
		
		asnEditText.setText("");
	}

	protected void saveAnswer(DS_Vote_detail_ques question) {
		//判断如果答案结果中已存， 就不要再存了
		for(DS_Vote_answer voteAnser:answerList){
			if(voteAnser.getUTID() == question.getUTID()){
				return;
			}
		}
		
		DS_Vote_answer answer = new DS_Vote_answer();
		
		answer.setNum(question.getNum());
		answer.setType(question.getType());
		answer.setUTID(question.getUTID());
		
		if(question.getType() == ConstAnts.VOTE_SINGLE_SELECT){
			if(selectA.isChecked()){
				answer.setAnswer("A");
			}else if(selectB.isChecked()){
				answer.setAnswer("B");
			}else if(selectC.isChecked()){
				answer.setAnswer("C");
			}else if(selectD.isChecked()){
				answer.setAnswer("D");
			}else if(selectE.isChecked()){
				answer.setAnswer("E");
			}else{
				answer.setAnswer("");
			}
			
		}else if(question.getType() == ConstAnts.VOTE_MULTI_SELECT){
			StringBuffer str = new StringBuffer();
			if(multiSelectA.isChecked()){
				str.append("A");
			}
			
			if(multiSelectB.isChecked()){
				if(str.toString().equals("")){
					str.append("B");
				}else{
					str.append(",B");
				}
			}
			
			if(multiSelectC.isChecked()){
				if(str.toString().equals("")){
					str.append("C");
				}else{
					str.append(",C");
				}
			}
			
			if(multiSelectD.isChecked()){
				if(str.toString().equals("")){
					str.append("D");
				}else{
					str.append(",D");
				}
			}
			if(multiSelectE.isChecked()){
				if(str.toString().equals("")){
					str.append("E");
				}else{
					str.append(",E");
				}
			}
			
			answer.setAnswer(str.toString());
		}else if(question.getType() == ConstAnts.VOTE_FILL_BLANK){
			answer.setAnswer(asnEditText.getText().toString());
		}
		answerList.add(answer);
	}

	private void submitAnswer(){
		BaseAjaxParams params = new BaseAjaxParams();
		params.put(HIF_SubmitVote.params.VotePaperID,voteID);
		
		Gson gson = new Gson();
		String jsonStr = gson.toJson(answerList);
		
		params.put(HIF_SubmitVote.params.AnswerJson,jsonStr);
		
		FinalHttp fh = new FinalHttp();
		fh.post(HIF_SubmitVote.address, params, new MyAjaxCallBack<Object>(this) {

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				
				HIF_SubmitVote.result result = JsonUtil.fromJson((String)t, HIF_SubmitVote.result.class);
				
				if(result != null){
					if(result.result == 1){
						showToast("提交成功！");
						
						Intent intent = new Intent();
						intent.setAction("com.brkj.votehavedone");
						intent.putExtra("voteID", voteID);
						VoteDetailActivity.this.sendBroadcast(intent);
						
						finish();
					}else{
						showToast("提交失败：" + result.reason);
					}
				}else{
					showToast("提交失败" );
				}
			}
			
		});
	}
	
}
